/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LigneVenteDialog.java
 *
 * Created on 14 nov. 2011, 08:54:11
 */
package app.views;

import app.App;
import app.Tools;
import app.models.Lignevente;
import app.models.Reference;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Ghost
 */
public class LigneVenteDialog extends javax.swing.JDialog implements 
        ChangeListener, KeyListener {

    //private Reference reference;
    private Lignevente lignesVente;
    private Lignevente defaultLv;
    
    private boolean error = false;
    
    private boolean editing = false;

    /** Creates new form LigneVenteDialog */
    public LigneVenteDialog(Reference ref) {
        super(App.getInstance().getMainFrame(), true);
        //center
        setLocationRelativeTo(null);
        lignesVente = new Lignevente();
        lignesVente.setReference(ref);
        lignesVente.setQuantite(1);
        lignesVente.setPxVente(ref.getPxVenteTTC());

        initComponents();
        
        if(ref.getStock() <= 0){
            App.displayMessage("Pas assez de ce produit en stock!", App.MESSAGE_TYPE.ERROR);
            
            error = true;
        } else {
            SpinnerNumberModel spinModel = new SpinnerNumberModel(1, 0, ref.getStock(), 1);
            spinModel.addChangeListener(this);

            txtQte.setModel(spinModel);
            txtReference.setText(lignesVente.getReference().getDesignation());
            txtPxUnit.setText(String.valueOf(lignesVente.getPxVente()));
            txtTotal.setText(String.valueOf(lignesVente.getTotal()));
        }
    }
    
    public LigneVenteDialog(Lignevente lv){
        super(new JFrame(), true);
        editing = true;
        
        setLocationRelativeTo(null);
        lignesVente = lv;
        defaultLv = lignesVente.clone();
        
        initComponents();
        Reference ref = lv.getReference();
        if(ref.getStock() <= 0){
            App.displayMessage("Pas assez de ce produit en stock!", App.MESSAGE_TYPE.ERROR);
            
            error = true;
        } else {
            SpinnerNumberModel spinModel = new SpinnerNumberModel(lv.getQuantite(), 0, ref.getStock(), 1);
            spinModel.addChangeListener(this);

            txtQte.setModel(spinModel);
            txtReference.setText(lignesVente.getReference().getDesignation());
            txtPxUnit.setText(String.valueOf(lignesVente.getPxVente()));
            txtTotal.setText(String.valueOf(lignesVente.getTotal()));
        }
        
    }
    
    public Lignevente showDialog(){
        if(error){
            return null;
        }
        this.setVisible(true);
        if(lignesVente == null) {
            return null;
        }
        if(lignesVente.getQuantite() == 0){
            return null;
        }
        return lignesVente;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btAnnuler = new javax.swing.JButton();
        btValider = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtReference = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtQte = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        txtPxUnit = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();

        setTitle("Combien?");
        setAlwaysOnTop(true);

        btAnnuler.setText("Annuler");
        btAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAnnulerActionPerformed(evt);
            }
        });

        btValider.setText("Valider");
        btValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btValiderActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btAnnuler)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                .addComponent(btValider)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btValider)
                    .addComponent(btAnnuler)))
        );

        jPanel2.setLayout(new java.awt.GridLayout(4, 2, 10, 10));

        jLabel2.setText("Référence:");
        jPanel2.add(jLabel2);

        txtReference.setEditable(false);
        txtReference.setText("jTextField1");
        jPanel2.add(txtReference);

        jLabel1.setText("Quantité:");
        jPanel2.add(jLabel1);
        jPanel2.add(txtQte);

        jLabel3.setText("Prix Unit. :");
        jPanel2.add(jLabel3);

        txtPxUnit.setText("jTextField2");
        txtPxUnit.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPxUnitFocusLost(evt);
            }
        });
        txtPxUnit.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtPxUnitKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPxUnitKeyTyped(evt);
            }
        });
        jPanel2.add(txtPxUnit);

        jLabel4.setText("Total:");
        jPanel2.add(jLabel4);

        txtTotal.setEditable(false);
        txtTotal.setText("jTextField1");
        jPanel2.add(txtTotal);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAnnulerActionPerformed
    if(editing){
        this.lignesVente = defaultLv;
    } else {
        this.lignesVente = null;
    }
    btValiderActionPerformed(evt);
}//GEN-LAST:event_btAnnulerActionPerformed

private void btValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btValiderActionPerformed
this.setVisible(false);
this.dispose();
}//GEN-LAST:event_btValiderActionPerformed

private void txtPxUnitKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPxUnitKeyTyped
    syncGui();
}//GEN-LAST:event_txtPxUnitKeyTyped

private void txtPxUnitFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPxUnitFocusLost
    syncGui();
}//GEN-LAST:event_txtPxUnitFocusLost

private void txtPxUnitKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPxUnitKeyReleased
    syncGui();
}//GEN-LAST:event_txtPxUnitKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAnnuler;
    private javax.swing.JButton btValider;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtPxUnit;
    private javax.swing.JSpinner txtQte;
    private javax.swing.JTextField txtReference;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables

    private void syncGui(){
        try {
            Double total = 0.0;
            Double pxUnit = Double.parseDouble(txtPxUnit.getText());
            int Qte = (Integer) txtQte.getValue();

            lignesVente.setPxVente(pxUnit);
            lignesVente.setQuantite(Qte);

            txtTotal.setText(String.valueOf(lignesVente.getTotal()));

        } catch (NumberFormatException nfe) {
            txtTotal.setText("NaN");
        } catch (Exception ex) {
            txtTotal.setText("NaN");
        }
    }
    
    @Override
    public void stateChanged(ChangeEvent e) {
        syncGui();
    }

    @Override
    public void keyTyped(KeyEvent e) {
        syncGui();
    }

    @Override
    public void keyPressed(KeyEvent e) {}

    @Override
    public void keyReleased(KeyEvent e) {}
}
